package com.androidhuman.example.mapsv2example;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



public class EventActivity extends Activity
{
	
	SharedPreferences userPrefs;
	SharedPreferences.Editor userPrefsEditor;


	
	TextView text;


	static Context mContext;

	private ListView listview;
	private MDataAdapter adapter;
	private LayoutInflater mInflater;
	private ArrayList<MData> alist;
	private Context context;

	
	int id;
	String event_info[][];
	String article_info[][];
	
	TextView e_title;
	TextView e_likenum;
	TextView e_text;
	TextView e_location;
	TextView e_d_place;
	TextView e_times;
	TextView e_user;
	LinearLayout click_like;
	LinearLayout click_reply;
	Bitmap gold;

	
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        mContext=this;
        Intent intent = getIntent();
		id = intent.getIntExtra("event_id", 0);
		
		String[][] event_info=UsingServer.getDataFromServer_E_detail(id, 2);
		article_info=UsingServer.getDataFromServer_E_detail(id, 1);
		
		e_title=(TextView)findViewById(R.id.headline);
		e_likenum=(TextView)findViewById(R.id.likenum);
		e_text=(TextView)findViewById(R.id.e_text);
		e_location=(TextView)findViewById(R.id.tv_location);
		e_d_place=(TextView)findViewById(R.id.tv_d_place);
		e_times=(TextView)findViewById(R.id.tv_times);
		e_user=(TextView) findViewById(R.id.e_user);
		click_like=(LinearLayout)findViewById(R.id.e_click_like);
		click_reply=(LinearLayout)findViewById(R.id.click_reply);
		
		
		
//		Toast toast=Toast.makeText(mContext, Integer.toString(id), Toast.LENGTH_SHORT);
//		toast.show();
		e_title.setText((event_info[0][3]));
		e_likenum.setText(event_info[0][6]);
		e_text.setText(event_info[0][5]);
		e_location.setText(event_info[0][10]);
		e_d_place.setText(event_info[0][11]);
		e_times.setText(event_info[0][7]);
		e_user.setText(event_info[0][12]);

		View btn_close = (View) findViewById(R.id.bt_close);
		btn_close.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				finish();
			}
		});


		mContext = getApplicationContext();


		
		
		listview = (ListView) findViewById(R.id.list_event);
		alist = new ArrayList<MData>();
		adapter = new MDataAdapter(this, alist);
		listview.setAdapter(adapter);

		context = this;

	
		click_like.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String str=UsingServer.likeUp(id, 2);
				e_likenum.setText(str);
			}
			
		});
		
		click_reply.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				int check = userPrefs.getInt("check", 0);

			//	if (check == 1) {
					Intent intent = new Intent(getApplicationContext(), ReportActivity.class);
					intent.putExtra("isFromEvent", true);
					intent.putExtra("eventID", id);
					startActivityForResult(intent, 1);

				//}
			}
			
		});
		


		if(article_info!=null)
		for(int i=0; i<article_info.length; i++){
			
			gold = UsingServer
					.getImageFromURL("http://dev.ts.hs.kr/trend/images/"
							+ "1_" + article_info[i][0] + "_.jpg");
			
		adapter.insert(new MData(this, 3, Integer.parseInt(TextChecker.purifyString(article_info[i][0])), 
				article_info[i][3], 
				article_info[i][4],article_info[i][5],
				Integer.parseInt(TextChecker.purifyString(article_info[i][6])),
				article_info[i][9],article_info[0][10],
				article_info[i][11],-100, gold), 0);}
		
		
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View arg1, int position,
					long arg3) {
				
				//Toast.makeText(context, Integer.toString(position) ,Toast.LENGTH_SHORT).show();
				
			}
		});
    
      
    }
    
    class MDataAdapter extends ArrayAdapter<MData> {
		private LayoutInflater mInflater;

		public MDataAdapter(Context context, ArrayList<MData> object) {
			super(context, 0, object);

			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		}

		public View getView(int position, View v, ViewGroup parent) {
			View view = null;
			if (v == null) {
				view = mInflater.inflate(R.layout.event_custom, null);
			} else {
				view = v;
			}
			final MData data = this.getItem(position);
			if (data != null) {
				
				TextView text = (TextView) view.findViewById(R.id.a_text);
				TextView address=(TextView) view.findViewById(R.id.a_tv_location);
				//TextView d_address=(TextView) view.findViewById(R.id.tv_d_place);
				final TextView likenum=(TextView) view.findViewById(R.id.a_likenum);
				TextView a_created=(TextView)view.findViewById(R.id.a_created);
				TextView a_user=(TextView) view.findViewById(R.id.a_user);
				//ImageView photo=(ImageView) view.findViewById(R.id.a_photo);
				ImageView goldman = (ImageView) view.findViewById(R.id.event_img);

				LinearLayout btn_like=(LinearLayout)view.findViewById(R.id.click_like);
				
				//Toast.makeText(mContext, data.getText(), Toast.LENGTH_SHORT).show();
				
				text.setText(data.getText());
				address.setText(data.getAddress());
				a_created.setText(data.getTime());
//				d_address.setText(data.getDetailAddress());
				likenum.setText(Integer.toString(data.getLikeNum()));
				a_user.setText(data.getUserName());
				
				 if (data.getImg() == null) {
				 goldman.setVisibility(View.GONE);
				 } else {
				 goldman.setVisibility(View.VISIBLE);
				 goldman.setImageBitmap(data.getImg());
				 }
				btn_like.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						String str=UsingServer.likeUp(data.getID(), 1);
						likenum.setText(str);
						data.setLikeNum(Integer.parseInt(TextChecker.purifyString(str)));
					}
					
				});
				
			}
			return view;
		}
	}

	private class MData {

		private int Type;
		private int article_id;			//0
		private String title;			//3
		private String time;			//4
		private String text;			//5
		private int like_num;			//6
		private String address;			//9
		private String detail_address;	//10
		private String user_name;		//11
		private Bitmap img;

		public MData(Context context, int mType, String mText, String mTime) {
			
			Type = mType;
			text = mText;

		}
		public MData(Context context, int mType, int article_id, String title,
				String time, String text, int like_num, String address,
				String detail_address, String user_name, int mPhoto, Bitmap mImg) {
			this.article_id=article_id;
			
			Type = mType;
			this.article_id=article_id;
			this.title=title;
			this.time=time;
			this.text=text;
			this.like_num=like_num;
			this.address=address;
			this.detail_address=detail_address;
			this.user_name=user_name;
			this.img=mImg;

		}

	
		public int getType(){
			return Type;
		}
		public int getID(){
			return article_id;
		}
		public String getTitle(){
			return title;
		}
		public String getTime(){
			return time;
		}
		public String getText(){
			return text;
		}
		public int getLikeNum(){
			return like_num;
		}
		public String getAddress(){
			return address;
		}
		public String getDetailAddress(){
			return detail_address;
		}
		public String getUserName(){
			return user_name;
		}
		public void setLikeNum(int like){
			like_num=like;
		}

		public Bitmap getImg(){
			return img;
		}
	}
	
	

}